<?php

namespace app\ruinuo\design;

use app\common\model\RuinuoApp;
use app\common\model\RuinuoForm;
use app\common\model\RuinuoMode;

class FormLayout
{

    public function GetForm($arr = [])
    {
        $str = '';
        foreach ($arr as $k => $v) {
            $v['elem_id'] = !empty($v['elem_id']) ? $v['elem_id'] : time() . '_' . $k;
            if ($v['tag'] == 'subtable') {
                $v['field'] = 'sub__a' . $k;
            }
            if ($v['tag'] == 'designtable') {
                $v['field'] = 'design__a' . $k;
            }
            switch ($v['tag']) {
                case "input":
                case "number":
                case "phone":
                case "email":
                case "password":
                case "tags":
                case "openselect":
                case "icon":
                case "datetime":
                case "upimg":
                case "upfile":
                case "jsonshow":
                case "showtree":
                case "map":
                case "gongshi":
                    $str .= $this->LineText($v);
                    break;
                case "textarea":
                case "htmledit":
                case "markdown":
                    $str .= $this->RowsText($v);
                    break;
                case "switch":
                    $str .= $this->Switch($v);
                    break;
                case "radio":
                    $str .= $this->Radio($v);
                    break;
                case "checkbox":
                    $str .= $this->Checkbox($v);
                    break;
                case "select":
                case "cascader":
                case "tree":
                case "dateall":
                    $str .= $this->Select($v);
                    break;
                case "color":
                    $str .= $this->Color($v);
                    break;
                case "rate":
                    $str .= $this->Rate($v);
                    break;
                case "slider":
                    $str .= $this->Slider($v);
                    break;
                case "tips":
                case "legend":
                    $str .= $this->TipsLegend($v);
                    break;
                case "grid":
                    $str .= $this->Grid($v);
                    break;
                case "card":
                    $str .= $this->Card($v);
                    break;
                case "tab":
                    $str .= $this->Tab($v);
                    break;
                case "flex":
                    $str .= $this->Flex($v);
                case "barcode":
                case "qrcode":
                    $str .= $this->CodeShow($v);
                    break;
                case "jsontable":
                case "subtable":
                case "choosetable":
                case "designtable":
                    $str .= $this->JsonTable($v);
                    break;
                default:
                    $str .= $this->LineText($v);
            }

        }
        return $str;
    }


    public function GetJs($arr = [])
    {
        $js_str = '';
        $arr = $this->GetFormField($arr);
        foreach ($arr as $k => $v) {
            $v['elem_id'] = !empty($v['elem_id']) ? $v['elem_id'] : time() . '_' . $k;

            $js_str .= $this->BindJs($v);
        }
        return $js_str;
    }

    public function BindJs($json = [])
    {
        $str = '';
        $tag = !empty($json['tag']) ? $json['tag'] : '';
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $field = !empty($json['field']) ? $json['field'] : $elem_id;
        switch ($tag) {
            case "showtree":
                $options_num = explode('|', $json['options_num']);
                $str = "layui.engine_form.BindShowTree('" . $elem_id . "','" . $field . "','" . $options_num[0] . "');" . PHP_EOL;
                break;
            case "datetime":
                $type = !empty($json['date_cat']) ? $json['date_cat'] : 'date';
                $range = !empty($json['range']) ? $json['range'] : false;
                $range_field = !empty($json['range_field']) ? $json['range_field'] : '';
                $str = "layui.engine_form.BindDateTime('" . $elem_id . "','" . $type . "','" . $range . "','" . $field . "','" . $range_field . "');" . PHP_EOL;
                break;
            case "dateall":
                $str = "layui.engine_form.BindDateAll('" . $elem_id . "','" . $field . "');" . PHP_EOL;
                break;
            case "color":
                $str = "layui.engine_form.BindColor('" . $elem_id . "');" . PHP_EOL;
                break;
            case "slider":
                $min_val = !empty($json['min_val']) ? $json['min_val'] : 1;
                $max_val = !empty($json['max_val']) ? $json['max_val'] : 100;
                $str = "layui.engine_form.BindSlider('" . $elem_id . "','" . $min_val . "','" . $max_val . "');" . PHP_EOL;
                break;
            case "rate":
                $str = "layui.engine_form.BindRate('" . $elem_id . "');" . PHP_EOL;
                break;
            case "openselect":
                $str = "layui.engine_form.BindOpenSelect('" . $elem_id . "');" . PHP_EOL;
                break;
            case "htmledit":
                $height = !empty($json['height']) ? $json['height'] : 400;
                $str = "layui.engine_form.BindHtmlEdit('" . $elem_id . "','" . $height . "');" . PHP_EOL;
                break;
            case "markdown":
                $height = !empty($json['height']) ? $json['height'] : 400;
                $str = "layui.engine_form.BindMarkDown('" . $elem_id . "','" . $height . "');" . PHP_EOL;
                break;

            case "icon":
                $str = "layui.engine_form.BindIcon('" . $elem_id . "');" . PHP_EOL;
                break;
            case "select":
            case "cascader":
            case "tree":
                $is_all = !empty($json['is_all']) ? $json['is_all'] : 2;
                if ($tag == 'select' && $is_all == 2) {
                    $str = '';
                } else {
                    $strict = !empty($json['strict']) ? $json['strict'] : false;
                    $options_num = explode('|', $json['options_num']);

                    $str = "layui.engine_form.BindSelect('" . $elem_id . "','" . $tag . "','" . $field . "','" . $options_num[0] . "',$is_all,$strict);" . PHP_EOL;
                }
                break;
            case "upimg":
            case "upfile":
                $cat = !empty($json['cat']) ? $json['cat'] : '';
                $size = !empty($json['size']) ? $json['size'] : '';
                $num = !empty($json['num']) ? $json['num'] : 1;
                $file = !empty($json['file']) ? $json['file'] : '';
                $tag = !empty($json['tag']) ? $json['tag'] : '';
                $str = "layui.engine_form.BindUpload('" . $elem_id . "','" . $file . "','" . $tag . "','" . $cat . "','" . $size . "','" . $num . "');" . PHP_EOL;
                break;
            case "tags":
                $placeholder = !empty($json['placeholder']) ? $json['placeholder'] : '回车确定';
                $str = "layui.engine_form.BindTags('" . $elem_id . "','" . $placeholder . "');" . PHP_EOL;
                break;
            case "qrcode":
            case "barcode":
                $code_field = !empty($json['code_field']) ? $json['code_field'] : $elem_id;
                $str = "layui.engine_form.BindCode('" . $elem_id . "','" . $code_field . "');" . PHP_EOL;
                break;
            case "jsontable":
            case "subtable":
            case "choosetable":
            case "designtable":
                $cols = !empty($json['cols']) ? $json['cols'] : [];
                foreach ($cols as $tk => $tv) {
                    $cols[$tk]['minWidth'] = !empty($tv['width']) ? $tv['width'] : 160;
                    unset($cols[$tk]['width']);
                }
                if (($tag == "subtable" || $tag == "choosetable") && !empty($json['mode_id'])) {
                    $Mode = RuinuoMode::where("id='" . $json['mode_id'] . "'")->field('app_id,num,name')->find();
                    $App = RuinuoApp::where("id='" . $Mode['app_id'] . "'")->field('num')->find();
                    $Form = RuinuoForm::where("mode_id='" . $json['mode_id'] . "'")->field('width,height')->find();
                    $url = "/" . $App['num'] . "/" . $Mode['num'] . "/";
                    $mode_sub_id = !empty($json['mode_sub_id']) ? $json['mode_sub_id'] : 'none';
                    $mode_sub_post = !empty($json['mode_sub_post']) ? $json['mode_sub_post'] : '';
                    $json['urls'] = [
                        'title' => $Mode['name'],
                        'add' => $url . 'add/?' . $mode_sub_id . '={id}&elem=table_' . $elem_id,
                        'edit' => $url . 'edit/?elem=table_' . $elem_id,
                        'delete' => $url . 'delete/',
                        'lists' => $url . 'index/?' . $mode_sub_id . '={id}&' . $mode_sub_post,
                        'width' => $Form['width'],
                        'height' => $Form['height'],
                    ];
                }
                if ($tag == "choosetable") {
                    $choose = !empty($json['is_all']) ? $json['is_all'] : 2;
                    $json['urls']['add'] = $url . 'index/?choose=' . $choose;
                    $json['urls']['lists'] = $url . 'index/';
                    $json['rule'] = $field . '||id';
                }
                $json['cols'] = $cols;
                $str = "let table" . $field . "=" . json_encode($json, JSON_UNESCAPED_UNICODE) . ";" . PHP_EOL;
                $str .= "layui.engine_form.BindJsonTable(table" . $field . ");" . PHP_EOL;
                break;
        }
        return $str;
    }


    public function Layout($json = [])
    {
        $required = !empty($json['required']) ? $json['required'] : '';
        $hide = !empty($json['hide']) ? $json['hide'] : '';
        $help = !empty($json['help']) ? $json['help'] : '';
        $suffix = !empty($json['suffix']) ? $json['suffix'] : '';
        $prefix = !empty($json['prefix']) ? $json['prefix'] : '';
        $readonly = !empty($json['readonly']) ? $json['readonly'] : 2;
        $group_suffix = !empty($json['group_suffix']) ? $json['group_suffix'] : '';
        $group_prefix = !empty($json['group_prefix']) ? $json['group_prefix'] : '';
        $col = !empty($json['col']) ? $json['col'] : '12';
        $title = !empty($json['title']) ? $json['title'] : '';
        $tag = !empty($json['tag']) ? $json['tag'] : '';
        $field = !empty($json['field']) ? $json['field'] : '';
        $hide_label = !empty($json['hide_label']) ? $json['hide_label'] : '';
        $value = !empty($json['value']) ? $json['value'] : '';
        $is_edit = !empty($json['is_edit']) ? $json['is_edit'] : '1';
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';

        switch ($tag) {
            case "datetime":
                $prefix = 'icon-riqi';
                break;
            case "email":
                $prefix = 'icon-youxiang';
                break;
            case "phone":
                $prefix = 'icon-shouji1';
                break;
            case "map":
                $prefix = 'icon-map';
                break;
            case "password":
                $prefix = 'icon-mima';
                break;
            case "dateall":
                $col .= " ruinuo_dateall";
                break;
        }
        if ($hide == 1 || $tag == 'ordernum') {
            $col .= " ruinuo_yes_none";
        }
        if ($field) {
            $col .= " " . $field . "_box";
        }
        $col .= " {{#  if(layui.engine_form.PowerField('" . $field . "')==1){ }}form_disabled{{#  } }}";
        $required_class = "";
        if ($required == 1) {
            $required_class = " layui-form-item-required";
        }
        $suffix_class = "";
        if ($group_suffix || $group_prefix) {
            $suffix_class .= " layui-input-group";
        }
        if ($suffix || $prefix) {
            $suffix_class .= " layui-input-wrap";
        }
        if ($readonly == 1) {
            $col .= ' form_disabled';
        }
        $str = '<div class="design-box layui-col-xs12 layui-col-md' . $col . '">' . PHP_EOL;
        if ($hide_label != 1) {
            $str .= '<label class="layui-form-label' . $required_class . '">' . $title . '</label>' . PHP_EOL;
        } else {
            $suffix_class .= ' block_margin_left_0';
        }


        $options_num = !empty($json['options_num']) ? $json['options_num'] : '';
        $options_num = explode('|', $options_num);
        $str .= '<div class="layui-input-block ' . $suffix_class . '" data_tag="' . $tag . '" data_source="' . $options_num[0] . '">' . PHP_EOL;
        if ($group_prefix) {
            $str .= '<div class="group_input group_prefix">' . $this->GetFix($group_prefix) . '</div>' . PHP_EOL;
        }
        if ($prefix) {
            $str .= '<div class="layui-input-prefix">' . $this->GetFix($prefix) . '</div>' . PHP_EOL;
        }
        $str .= '<content>';
        if ($group_suffix) {
            $str .= '<div class="group_input group_suffix">' . $this->GetFix($group_suffix) . '</div>' . PHP_EOL;
        }
        if ($suffix) {
            $str .= '<div class="layui-input-suffix layui-input-split">' . $this->GetFix($suffix) . '</div>' . PHP_EOL;
        }
        if ($tag == 'upimg' || $tag == 'upfile') {
            $c = '文件';
            if ($tag == 'upimg') {
                $c = '图片';
            }
            $is_file = !empty($json['is_file']) ? $json['is_file'] : 1;
            $file_num = !empty($json['file_num']) ? $json['file_num'] : 2;
            if ($is_file == 1) {
                $file_select = !empty($json['file_select']) ? $json['file_select'] : 2;
                $is_shot = !empty($json['is_shot']) ? $json['is_shot'] : 2;

                $str .= '<div class="upfile display_flex">' . PHP_EOL;
                $str .= '<button class="pear-btn pear-btn-sm" type="button" id="upload_' . $json['elem_id'] . '">';
                $str .= '<i class="layui-icon layui-icon-addition"></i>上传' . $c;
                $str .= '</button>' . PHP_EOL;
                if ($file_select == 1) {
                    $str .= '<button class="pear-btn pear-btn-sm" type="button" onclick="layui.engine_form.ChooseFile(\'' . $elem_id . '\' , \'' . $tag . '\', \'' . $file_num . '\')">';
                    $str .= '<i class="layui-icon layui-icon-spread-left"></i>选择' . $c;
                    $str .= '</button>' . PHP_EOL;
                }
                if ($is_shot == 1) {
                    $str .= '<button class="pear-btn pear-btn-sm" type="button" onclick="layui.engine_form.BindVideo(\'' . $elem_id . '\' , \'' . $tag . '\', \'' . $file_num . '\')">';
                    $str .= '<i class="ruinuo-icon icon-video"></i>开始拍摄';
                    $str .= '</button>' . PHP_EOL;
                }


                if ($help) {
                    $str .= ' <div class="layui-form-help" style="padding-top: 0px;margin-left: 5px"><i class="layui-icon layui-icon-tips"></i>' . $help . '</div>' . PHP_EOL;
                }
                $str .= '</div>' . PHP_EOL;
            }
            $str .= '<div class="upload_con"></div>' . PHP_EOL;
        }
        if ($tag == 'showtree') {
            $str .= ' <div id="showtree_' . $json['elem_id'] . '"></div>' . PHP_EOL;
        }
        if ($help && $tag != 'upimg' && $tag != 'upfile') {
            $str .= ' <div class="layui-form-help"><i class="layui-icon layui-icon-tips"></i>' . $help . '</div>' . PHP_EOL;
        }
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        return $str;
    }

    public function GetFix($str = '')
    {
        $icon = explode('icon', $str);
        if (count($icon) == 1) {
            return $str;
        } else {
            return '<i class="ruinuo-icon ' . $str . '"></i>';
        }
    }

    public function Attribute($json = [])
    {
        $readonly = !empty($json['readonly']) ? $json['readonly'] : '';
        $disabled = !empty($json['disabled']) ? $json['disabled'] : '';
        $follow = !empty($json['follow']) ? $json['follow'] : '';
        $field = !empty($json['field']) ? $json['field'] : '';
        $tag = !empty($json['tag']) ? $json['tag'] : '';
        $type = !empty($json['type']) ? $json['type'] : 'text';
        $placeholder = !empty($json['placeholder']) ? $json['placeholder'] : '';
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';

        switch ($tag) {
            case "password":
                $type = 'password';
                break;
            case "number":
                $type = 'number';
                break;
            case "textarea":
            case "htmledit":
            case "markdown":
                $type = 'textarea';
                break;
            case "rate":
            case "showtree":
                $type = 'hidden';
                break;
            case "icon":
            case "upimg":
            case "upfile":
            case "jsonkey":
                $readonly = 1;
                $type = 'hidden';
                break;
            case "datetime":
                $readonly = 1;
                $range_field = !empty($json['range_field']) ? $json['range_field'] : '';
                if ($range_field) {
                    $field = '';
                }
                break;
            case "tags":
                $placeholder = '回车确定';
                break;
            case "map":
            case "gongshi":
            case "ordernum":
                $readonly = 1;
                break;
        }

        $str = "";
        if ($tag == 'openselect') {
            $readonly = 1;
            $type = "hidden";

        }

        if ($tag == 'map') {
            $readonly = 1;
            $str .= ' onclick="layui.engine_form.BindMap(this)"';
        }


        if ($field) {
            $str .= ' name="' . $field . '"';
        }
        $str .= ' id="focus_' . $elem_id . '"';
        if ($type) {
            $str .= ' type="' . $type . '"';
        }

        if ($readonly == 1) {
            $str .= ' readonly';
        }
        if ($disabled == 1) {
            $str .= ' disabled';
        }

        if ($placeholder) {
            $str .= ' placeholder="' . $placeholder . '"';
        }

        if ($follow == 1) {
            $str .= ' onclick="layui.engine_form.GetFollow(this)"';
            $str .= ' readonly';
        }

        if ($tag == 'number') {
            $str .= ' lay-affix="number"';
        }
        if ($tag == 'password') {
            $str .= ' lay-affix="eye"';
        }
        $str .= ' lay-tag="' . $tag . '"';
        return $str;
    }


    public function LineText($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $range_field = !empty($json['range_field']) ? $json['range_field'] : '';
        $range_value = !empty($json['range_value']) ? $json['range_value'] : '';
        $range = !empty($json['range']) ? $json['range'] : '至';
        $show_value = !empty($json['value']) ? $json['value'] : '';
        $field = !empty($json['field']) ? $json['field'] : '';
        $tag = !empty($json['tag']) ? $json['tag'] : '';
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $content = '';
        $t_html = '';
        if ($tag == 'datetime' && $range_field) {
            $t_html .= '<input type="hidden" name="' . $field . '"  value="' . $show_value . '">';
            $t_html .= '<input type="hidden" name="' . $range_field . '"  value="' . $range_value . '">';
        }
        if ($tag == 'openselect') {
            $choose = !empty($json['is_all']) ? $json['is_all'] : 2;
            $url = !empty($json['open_url']) ? $json['open_url'] : '';
            $c = explode("?", $url);
            if (count($c) == 2) {
                $url .= "&choose=" . $choose;
            } else {
                $url .= "?choose=" . $choose;
            }

            $event_val = !empty($json['event_val']) ? $json['event_val'] : [];
            $val = '';
            foreach ($event_val as $kk => $vv) {
                $val .= $vv['save_field'] . '||' . $vv['show_field'] . ',';
            }
            $val = rtrim($val, ",");
            $data_json = ' data_url="' . $url . '" data_tag="' . $tag . '" data_rule="' . $val . '"';
            $t_html .= '<div class="display_flex"><div id="open_' . $elem_id . '" class="pear-btn pear-btn-sm btn_theme_1" ' . $data_json . ' onclick="layui.engine_table.ChooseTable(\'' . $elem_id . '\')" ><i class="ruinuo-icon icon-add-bold"></i> 选择</div>';
            $t_html .= '<div class="choose_list flex_1"></div></div>';
        }
        $content .= '<input ' . $attribute . ' autocomplete="off"  value="{{= d.detail.' . $field . '}}" class="layui-input">' . $t_html . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function RowsText($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $height = !empty($json['height']) ? $json['height'] : '90';
        $style = 'style="min-height: ' . $height . 'px;"';
        $field = !empty($json['field']) ? $json['field'] : '';
        $content = '';
        if ($json['tag'] == 'markdown') {
            $content .= '<div id="markdown_' . $json['elem_id'] . '">';
        }
        $content .= '<textarea ' . $attribute . ' autocomplete="off" class="layui-textarea" ' . $style . '>{{= d.detail.' . $field . '}}</textarea>';
        if ($json['tag'] == 'markdown') {
            $content .= '</div>';
        }
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Switch($json = [])
    {
        $layout = $this->Layout($json);
        $json['text'] = !empty($json['text']) ? $json['text'] : '是|否';
        $content = '<input type="checkbox" {{#  if(d.detail.' . $json['field'] . ' ==1){ }}checked{{#  } }} value="1" name="' . $json['field'] . '" lay-filter="switch_filter" lay-skin="switch" lay-text="' . $json['text'] . '"  lay-tag="' . $json['tag'] . '"/>';
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Radio($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $options_num = explode('|', $json['options_num']);
        $content = '{{#  layui.each(d.source.' . $options_num[0] . '.rows, function(index, item){ }}' . PHP_EOL;
        $content .= '<input type="radio" ' . $attribute . ' value="{{= item.value }}" {{#  if(item.value==d.detail.' . $json['field'] . '){ }}checked{{#  } }} title="{{= item.label }}" lay-filter="radio_filter"  lay-tag="' . $json['tag'] . '">' . PHP_EOL;
        $content .= '{{#  }); }}' . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Checkbox($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $options_num = explode('|', $json['options_num']);
        $content = '{{#  layui.each(d.source.' . $options_num[0] . '.rows, function(index, item){ }}' . PHP_EOL;
        $content .= "{{# var value =layui.engine_form.CheckboxVal(item.value,d.detail." . $json['field'] . ");}}" . PHP_EOL;
        $content .= '<input type="checkbox" ' . $attribute . ' value="{{= item.value }}" {{#  if(value==1){ }}checked{{#  } }} title="{{= item.label }}" lay-filter="checkbox_filter"  lay-tag="' . $json['tag'] . '">' . PHP_EOL;;
        $content .= '{{#  }); }}' . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Select($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $content = '<div ' . $attribute . '></div>' . PHP_EOL;
        $is_all = !empty($json['is_all']) ? $json['is_all'] : 2;
        $tag = !empty($json['tag']) ? $json['tag'] : '';
        $field = !empty($json['field']) ? $json['field'] : $json['elem_id'];
        if ($is_all != 1 && $tag == 'select') {
            $placeholder = !empty($json['placeholder']) ? $json['placeholder'] : '请选择';
            $no_value = !empty($json['no_value']) ? $json['no_value'] : 2;
            $options_num = explode('|', $json['options_num']);
            $content = '<select name="' . $field . '" lay-filter="select_filter" lay-search="">';
            if ($no_value == 2) {
                $content .= '<option value="">' . $placeholder . '</option>' . PHP_EOL;
            }
            if (!empty($options_num[0])) {
                $content .= '{{- layui.engine_form.SelectOption(d.source.' . $options_num[0] . '.rows,d.detail.' . $json['field'] . ')}}';
            }

            $content .= '</select>' . PHP_EOL;;
        }
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Color($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $content = '<input ' . $attribute . ' autocomplete="off"  value="{{= d.detail.' . $json['field'] . '}}" class="layui-input">' . PHP_EOL;;
        $content .= '<div class="focus_' . $json['elem_id'] . ' colorpicker" data-id="focus_' . $json['elem_id'] . '"></div>' . PHP_EOL;;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Rate($json = [])
    {
        $layout = $this->Layout($json);
        $attribute = $this->Attribute($json);
        $content = '<input ' . $attribute . ' autocomplete="off"  value="{{= d.detail.' . $json['field'] . '}}" class="layui-input">' . PHP_EOL;;
        $content .= '<div class="focus_' . $json['elem_id'] . ' ratepicker" data-id="focus_' . $json['elem_id'] . '"></div>' . PHP_EOL;;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Slider($json = [])
    {
        $layout = $this->Layout($json);
        $content = '<input type="hidden" id="focus_' . $json['elem_id'] . '" name="' . $json['field'] . '"  value="{{= d.detail.' . $json['field'] . '}}" class="layui-input" >' . PHP_EOL;
        $content .= '<div id="slider_' . $json['elem_id'] . '" data-id="focus_' . $json['elem_id'] . '"></div>' . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function TipsLegend($json = [])
    {
        $str = "";
        $col = !empty($json['col']) ? $json['col'] : 12;
        $content = !empty($json['content']) ? $json['content'] : 12;
        switch ($json['tag']) {
            case "tips":
                $str = '<blockquote class="design-box layui-elem-quote tips_legend layui-col-xs' . $col . '">' . $content . '</blockquote>' . PHP_EOL;
                break;
            case "legend":
                $str = '<fieldset class="design-box layui-elem-field layui-field-title layui-col-xs' . $col . '"><legend class="tips_legend">' . $content . '</legend></fieldset>' . PHP_EOL;
                break;
        }
        return $str;
    }

    public function Grid($json = [])
    {
        $col = !empty($json['col']) ? $json['col'] : 12;
        $tabcols = !empty($json['children']) ? $json['children'] : [];
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $str = ' <div class="design-box layui-col-xs12 layui-col-md' . $col . '">' . PHP_EOL;
        foreach ($tabcols as $k => $v) {
            $tabcols[$k]['num'] = !empty($v['num']) ? $v['num'] : 6;
            $tabcols[$k]['children'] = !empty($v['children']) ? $v['children'] : [];
            $str .= '<div class="design-rows layui-col-xs12 layui-col-md' . $tabcols[$k]['num'] . '">' . PHP_EOL;
            $str .= '<div class="j-fixed-grid-design ays-ctrl-container layui-row layui-col-space10" id="' . $elem_id . '_s_' . $k . '">' . PHP_EOL;
            $str .= $this->GetForm($tabcols[$k]['children']);
            $str .= '</div>' . PHP_EOL;
            $str .= '</div>' . PHP_EOL;
        }
        $str .= '</div>';
        return $str;
    }

    public function Card($json = [])
    {
        $col = !empty($json['col']) ? $json['col'] : 12;
        $tabcols = !empty($json['children']) ? $json['children'] : [];
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $str = '<div class="design-box layui-col-xs12 layui-col-md' . $col . '">' . PHP_EOL;
        $str .= '<div class="layui-card form_card">' . PHP_EOL;
        $str .= '<div class="layui-card-header">' . $json['title'] . '</div>' . PHP_EOL;
        $str .= '<div class="layui-card-body j-fixed-grid-design ays-ctrl-container layui-row layui-col-space10" id="' . $elem_id . '_s_0">' . PHP_EOL;
        $str .= $this->GetForm($tabcols[0]['children']);
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        return $str;
    }

    public function Tab($json = [])
    {
        $col = !empty($json['col']) ? $json['col'] : 12;
        $tabcols = !empty($json['children']) ? $json['children'] : [];
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $str = '<div class="design-box layui-col-xs12 layui-col-md' . $col . '">';
        $str .= '<div class="layui-tab layui-tab-brief">' . PHP_EOL;
        $str .= '  <ul class="layui-tab-title">' . PHP_EOL;
        foreach ($tabcols as $k => $v) {
            $tabcols[$k]['num'] = !empty($v['num']) ? $v['num'] : 6;
            $tabcols[$k]['children'] = !empty($v['children']) ? $v['children'] : [];
            $css = empty($k) ? 'layui-this' : '';
            $str .= '<li class="' . $css . '">' . $v['name'] . '</li>' . PHP_EOL;
        }
        $str .= '</ul>';
        $str .= '<div class="layui-tab-content">';
        foreach ($tabcols as $k => $v) {
            $css = empty($k) ? 'layui-show' : '';
            $str .= '<div class="layui-tab-item ' . $css . ' j-fixed-grid-design ays-ctrl-container layui-row layui-col-space10" id="' . $elem_id . '_s_' . $k . '">' . PHP_EOL;
            $str .= $this->GetForm($v['children']);
            $str .= '</div>' . PHP_EOL;
        }
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        return $str;
    }

    public function Flex($json = [])
    {

        $flex = !empty($json['children']) ? $json['children'] : [];
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $col = !empty($json['col']) ? $json['col'] : 12;
        $str = '<div class="design-box layui-col-xs12 layui-col-md' . $col . '">' . PHP_EOL;
        $str .= '<div class="j-fixed-grid-design ays-ctrl-container layui-row layui-col-space10 ruinuo_flex" id="' . $elem_id . '_s_0">' . PHP_EOL;
        $str .= $this->GetForm($flex[0]['children']) . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        $str .= '</div>' . PHP_EOL;

        return $str;
    }

    public function CodeShow($json = [])
    {

        $max_width = !empty($json['max_width']) ? $json['max_width'] : 180;
        $code_field = !empty($json['code_field']) ? $json['code_field'] : 'code_field' . $json['elem_id'];
        $url = '/common/plugins/qrcode/?text={{= d.detail.' . $code_field . '}}';
        if ($json['tag'] == 'barcode') {
            $url = '/common/plugins/barcode/?text={{= d.detail.' . $code_field . '}}';
        }
        $layout = $this->Layout($json);
        $content = '<img data-field="' . $code_field . '" style="width:' . $max_width . 'px" id="code_' . $json['elem_id'] . '"" src="' . $url . '">' . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }

    public function Html($json = [])
    {

        $html_content = !empty($json['html_content']) ? $json['html_content'] : '';
        $col = !empty($json['col']) ? $json['col'] : 12;
        $str = '<div class="layui-col-xs12 layui-col-md' . $col . '">' . PHP_EOL;
        $str .= $html_content . PHP_EOL;
        $str .= '</div>' . PHP_EOL;
        return $str;
    }

    public function JsonTable($json = [])
    {
        $layout = $this->Layout($json);
        $elem_id = !empty($json['elem_id']) ? $json['elem_id'] : '';
        $field = !empty($json['field']) ? $json['field'] : '';
        $tableid = "table_" . $elem_id;
        $content = '<textarea name="' . $field . '" id="focus_' . $elem_id . '" class="layui-input" style="display: none"  lay-filter="json_filter">{{= d.detail.' . $field . '}}</textarea>' . PHP_EOL;
        $content .= '<div class="' . $tableid . ' f_table">' . PHP_EOL . '<table id="' . $tableid . '" lay-filter="filter_' . $elem_id . '"></table>' . PHP_EOL . '</div>' . PHP_EOL;
        $str = str_replace('<content>', $content, $layout);
        return $str;
    }


    public function GetFormField($arr = [])
    {
        $item = [];
        foreach ($arr as $k => $v) {

            if ($v['tag'] == 'subtable') {
                $v['field'] = 'sub__a' . $k;
            }

            if ($v['tag'] == 'designtable') {
                $v['field'] = 'design__a' . $k;
            }
            if (empty($v['field'])) {
                $children = !empty($v['children']) ? $v['children'] : [];
                foreach ($children as $k2 => $v2) {
                    $children2 = !empty($v2['children']) ? $v2['children'] : [];
                    foreach ($children2 as $k3 => $v3) {
                        $item[] = $v3;
                    }
                }
            } else {
                $item[] = $v;
            }
        }
        return $item;
    }


}